package com.ssy.bus.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ssy.bus.pojo.Patient;
import com.ssy.bus.service.IPatientService;
import com.ssy.bus.vo.PatientVo;
import com.ssy.common.CodeMsg;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author ssy
 */
@RestController
@RequestMapping("patient")
public class PatientController {

    @Autowired
    private IPatientService patientService;

    /**
     * 患者库的分页查询
     *
     * @param patientVo 查询条件
     * @return data
     */
    @GetMapping("/loadPatientPage")
    public PageResult loadPatientPage(PatientVo patientVo) {
        return this.patientService.selectPatientByPage(patientVo);
    }

    /**
     * 根据身份证号查询患者
     *
     * @param idNumber 身份证号
     * @return data
     */
    @GetMapping("/selectOnePatient/{idNumber}")
    public Result selectOnePatient(@PathVariable String idNumber) {
        return this.patientService.selectPatientByIdNumber(idNumber);
    }

    /**
     * 添加患者
     *
     * @param patient patient
     * @return data
     */
    @PostMapping("/addPatient")
    public Result addPatient(Patient patient) {
        patient.setCreateTime(new Date());
        Patient patient1 = this.patientService.getOne(new LambdaQueryWrapper<Patient>().eq(Patient::getIdNumber, patient.getIdNumber()));
        if (patient1 != null) {
            this.patientService.update(patient, new LambdaQueryWrapper<Patient>().eq(Patient::getIdNumber, patient.getIdNumber()));
        } else {
            this.patientService.save(patient);
        }

        return new Result(CodeMsg.ADD_SUCCESS);
    }

}
